package org.example.memora.service.serviceImpl;

import org.apache.ibatis.annotations.Results;
import org.example.memora.common.result.PageResult;
import org.example.memora.common.result.Result;
import org.example.memora.common.utils.UserHolder;
import org.example.memora.entity.Community;
import org.example.memora.entity.CommunityUser;
import org.example.memora.mapper.CommunityMapper;
import org.example.memora.mapper.CommunityUserMapper;
import org.example.memora.service.CommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class CommunityServiceImpl implements CommunityService {

    @Autowired
    private CommunityMapper communityMapper;

    @Autowired
    private CommunityUserMapper communityUserMapper;
    @Override
    public boolean isMember(Long communityId, Long userId) {
        return communityUserMapper.selectByCommunityIdAndUserId(communityId, userId) != null;
    }

    @Override
    public Result<?> createCommunity(Community community) {
        int result=communityMapper.insert(community);
        CommunityUser communityUser=new CommunityUser();
        communityUser.setUserId(community.getOwnerId());
        communityUser.setRole("owner");
        communityUser.setJoinedAt(community.getCreatedAt());
        communityUser.setCommunityId(community.getId());
        communityUserMapper.insert(communityUser);
        if(result==0){
            return Result.error("插入失败");
        }
        return Result.success();
    }

    @Override
    public Result<?> getCommunityById(Long id) {
        Community community=communityMapper.selectById(id);
        return Result.success(community);
    }

    @Override
    public List<Community> getAllCommunities(int offset, int limit) {
        return communityMapper.selectAllWithPagination(offset, limit);
    }

    @Override
    public Result<?> getMembersByCommunityId(Long communityId) {
         List<CommunityUser> list=communityUserMapper.selectByCommunityId(communityId);
        return Result.success(list);
    }

    @Override
    public PageResult listCommunities(int page, int size) {
        int offset = (page - 1) * size;
        Long userId=UserHolder.getUser().getUserId();
        List<Community> records = communityMapper.selectAllWithPaginationByUserId(offset, size, userId);
        long total = communityMapper.countAll();
        return new PageResult(total, records);
    }
    @Override
    public PageResult listAllCommunities(Integer page, Integer size){
        int offset = (page - 1) * size;
        List<Community> records = communityMapper.selectAllWithPagination(offset, size);
        long total = communityMapper.countAll();
        return new PageResult(total, records);

    }

    @Override
     public Long getOwnerIdByCommunityId(Long communityId){
        return communityMapper.selectById(communityId).getOwnerId();
    }

}

